package com.baidu.app.service;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baidu.app.dao.AppMapper;
import com.baidu.app.pojo.App;
import com.baidu.app.pojo.AppType;
import com.baidu.app.pojo.Picture;
import com.baidu.util.FreemarkerUtil;
import com.baidu.util.HttpUtil;
import com.baidu.util.QrCodeImageUtil;

@Service
public class AppServiceImpl implements AppServiceI {

	@Autowired
	private AppMapper mapper;

	
	@Override
	public void addhtmlParserOne(String appurl) {
			try {
		      
				Parser parser = new Parser(appurl);
			    parser.setEncoding("UTF-8");
			    		    
			    String id = UUID.randomUUID().toString().replace("-", "");
			    //抓取appname
			    TagNameFilter tagNameFilter = new TagNameFilter("title");
			    NodeList list = parser.parse(tagNameFilter);
			    String appname = list.elementAt(0).toPlainTextString().split("-")[0];
			 
			    String imagePath = "/image/app/" + appname + "/";
			  //   File file = new File("E:\\app\\" + appname);
			     File file = new File("D:\\apache-tomcat-7.0.14\\upload\\app\\" + appname);
			     if(!file.exists() && !file.isDirectory()){
			    	 file.mkdirs();
			     }
			
			    //抓取appicon
			    parser.reset();
			    HasAttributeFilter attributeFilter = new HasAttributeFilter("id", "appicon");
			    list = parser.parse(attributeFilter);
			    String appiconurl = ((TagNode)list.elementAt(0)).getAttribute("src");
			    //下载appicon		    
			    String[] icons = appiconurl.split("/");
			    HttpUtil.httpDownload(appiconurl, file + "\\" + icons[icons.length-1]);
			     
			    
			   //抓取app简介
			   parser.reset();
			   attributeFilter = new HasAttributeFilter("class", "mj_yyjs font-f-yh");
			   list = parser.parse(attributeFilter);
			   String description = list.elementAt(0).toPlainTextString();
			  
			   
			   //抓取app二维码
			   parser.reset();
			   attributeFilter = new HasAttributeFilter("class", "mj_ew");
			   list = parser.parse(attributeFilter);
			   String codeurl = ((TagNode)list.elementAt(0).getFirstChild()).getAttribute("src");
			   String[] split = codeurl.split("/");
			//   HttpUtil.httpDownload(codeurl, file + "\\" + split[split.length-1]);
			   
			   //生成二维码
			   QrCodeImageUtil.getCode("ssss", file + "\\" + icons[icons.length-1], file + "\\" + split[split.length-1]);
			   codeurl = imagePath + split[split.length-1];
			   
			   
			   //抓取基本信息
			   parser.reset();
			   attributeFilter = new HasAttributeFilter("class", "mj_info font-f-yh");
			   list = parser.parse(attributeFilter);
			   NodeList children = list.elementAt(0).getFirstChild().getChildren();
		       Map<Integer, String> map = new HashMap<Integer, String>();
			   for (int i = 1; i < children.size()-1; i++) {
	                split = children.elementAt(i).toPlainTextString().split("：");
	                map.put(i, split[1].replaceAll("\\s", "")); 
			   }
		
			   
			   //价格
			   Double price = null;
			   String price2= map.get(1);
			   if(!price2.matches("[\u4e00-\u9fa5]+")){
				  price = new Double(price2);
			   }

			   
			   //版本
			   String version = map.get(2);
			 
			   //大小
			   String size = map.get(3);
			   
			   //开发者
			   String developer = map.get(4);
			   
			   //所属类别
			   String apptype = map.get(5);
			   String type = apptype.substring(0, 2);
			   String typeName = apptype.substring(2);
			   List<AppType> appTypeList = mapper.getAppTypeByName(typeName);	   
			   System.out.println(typeName);
			   AppType getAppType = null;
			   if(appTypeList != null && !appTypeList.isEmpty()){
				    getAppType = appTypeList.get(0);
			   }
			   AppType newType = null;
			   if(getAppType == null){
				   if(type.equals("软件")){
					      newType = new AppType(id, typeName, "1", "");
				   } else if(type.equals("游戏")) {
					      newType = new AppType(id, typeName, "2", "");
				   } else {
					     newType = new AppType(id, typeName, "0", "");
				   }
				  mapper.insertAppType(newType);
			   }
			  
			   
			   
			   
			  //更新时间
			  String updatetime = map.get(6);
			  
			  //所属平台
			  String platform = map.get(7);
			  
			
			  //抓取apk
			  parser.reset();
			  attributeFilter = new HasAttributeFilter("class", "mj_xzdbd");
			  list = parser.parse(attributeFilter);
			  if(list != null && 0 != list.size()){
				  String apkurl = ((TagNode)list.elementAt(0)).getAttribute("href");
			      HttpUtil.httpDownload(apkurl, file + "\\" + appname + ".apk");
			  }
			  
			  
			  
		     
			  
		
			  List<Picture> pictureList = new ArrayList<Picture>();
			  //抓取应用图片1
		      parser.reset();
			  attributeFilter = new HasAttributeFilter("id", "mj_tu_1");
			  list = parser.parse(attributeFilter);
			  Picture picture=null;
			  if(list != null && 0 != list.size()){
				  String picture1Url = ((TagNode)list.elementAt(0).getFirstChild()).getAttribute("src");
				  split = picture1Url.split("/");
				  HttpUtil.httpDownload(picture1Url, file + "\\" + split[split.length-1]);
				  picture  = new Picture(id + "1", imagePath + split[split.length-1], id);
			      mapper.insertPicture(picture);
			      pictureList.add(picture);
			  }
			  
		  
			  
			  
			  
			  
			  
			  
			  
			  
			  
			  //抓取应用图片2
			  parser.reset();
			  attributeFilter = new HasAttributeFilter("id", "mj_tu_2");
			  list = parser.parse(attributeFilter);
			  Picture picture2 = null;
			  if(list != null && 0 != list.size()) {
				  String picture2Url = ((TagNode)list.elementAt(0).getFirstChild()).getAttribute("src");
				  split = picture2Url.split("/");
				  HttpUtil.httpDownload(picture2Url, file + "\\" + split[split.length-1]);
				  picture2 = new Picture(id + "2", imagePath + split[split.length-1], id);
			      mapper.insertPicture(picture2);
			      pictureList.add(picture2);
			  }
			 
		
		  
			  
			  //抓取应用图片3
			  parser.reset();
			  attributeFilter = new HasAttributeFilter("id", "mj_tu_3");
			  list = parser.parse(attributeFilter);
			  Picture picture3 =null;
			  if(list != null && 0 != list.size()){
				  String picture3Url = ((TagNode)list.elementAt(0).getFirstChild()).getAttribute("src");
				  split = picture3Url.split("/");
				  HttpUtil.httpDownload(picture3Url, file + "\\" + split[split.length-1]);
				  picture3 = new Picture(id + "3", imagePath + split[split.length-1], id);
			      mapper.insertPicture(picture3);
			      pictureList.add(picture3);
			  }
			  
			
			 
	  
			  //将app数据插入数据库		  
			  App app = new App();
			  app.setId(id);
			  app.setAppname(appname);
			  app.setVersion(version);
			  app.setAppicon(imagePath + icons[icons.length-1]);
			  app.setApkurl(imagePath + appname + ".apk");
			  app.setDescription(description);
			  app.setFilesize(size);
			  app.setUpdatetime(new SimpleDateFormat("yyyy-MM-dd").parse(updatetime));
			  app.setDeveloper(developer);
			  app.setApptype(id);
			  app.setCodeurl(codeurl);
			  if(getAppType!=null){
				  app.setApptype(getAppType.getId());
			  } else {
				  app.setApptype(newType.getId());
			  }
			  app.setPrice(price);
			  app.setPlatform(platform);
			  mapper.insertSelective(app);
			 
			  
			  //生成html页面		  
			  Map<String, Object> root = new HashMap<String, Object>();
			  app.setApptype(type + " " + typeName);
			  root.put("app", app); 
		//	  root.put("imageList", pictureList);
			  root.put("picture", picture);
			  root.put("picture2", picture2);
			  root.put("picture3", picture3);
			  root.put("basePath","/image/css");
			  FreemarkerUtil freemarker = new FreemarkerUtil();
		//	  freemarker.print("detailed.ftl", root);
			  freemarker.createHtml("moban.ftl", root, "D:\\apache-tomcat-7.0.14\\upload\\" + id + ".html");

	    
		    		  
			} catch (Exception e) {
				e.printStackTrace();
			}
			
	
		}


	
	
	
}
